package com.xts.weinfo.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.xts.weinfo.R;
import com.xts.weinfo.base.BaseActivity;
import com.xts.weinfo.base.BaseApp;
import com.xts.weinfo.base.Constants;
import com.xts.weinfo.bean.FinishEvent;
import com.xts.weinfo.presenter.RegisterPresenter;
import com.xts.weinfo.view.RegisterView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册页面,也是完善资料页面
 * 所以启动的时候要传标识去区分
 */
public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterView {

    //注册
    public static final int TYPE_REGISTER = 0;
    //完善资料
    public static final int TYPE_INPUT_INFO = 1;


    @BindView(R.id.toolBar)
    Toolbar mToolBar;
    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.et_psd)
    EditText mEtPsd;
    @BindView(R.id.btn_register)
    Button mBtnRegister;
    private int mType;
    private String mUid;
    private String mTypeId;

    /**
     * 启动activity全部通过这个方法去启动,在这个方法里面明确启动这个页面需要的参数
     *
     * @param context
     */
    public static void startAct(Context context, int type) {
        Intent intent = new Intent(context, RegisterActivity.class);
        intent.putExtra(Constants.TYPE, type);
        context.startActivity(intent);
    }

    public static void startAct(Context context, int type,String uid,String typeId) {
        Intent intent = new Intent(context, RegisterActivity.class);
        intent.putExtra(Constants.TYPE, type);
        intent.putExtra(Constants.USERID,uid);
        intent.putExtra(Constants.TYPEID,typeId);
        context.startActivity(intent);
    }

    @Override
    protected RegisterPresenter initPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

        mType = getIntent().getIntExtra(Constants.TYPE, TYPE_REGISTER);
        //三方平台的唯一标识
        mUid = getIntent().getStringExtra(Constants.USERID);
        mTypeId = getIntent().getStringExtra(Constants.TYPEID);
        if (mType == TYPE_REGISTER) {
            mToolBar.setTitle(R.string.register);
            mBtnRegister.setText(R.string.register);
        }else {
            mToolBar.setTitle(R.string.input_info);
            mBtnRegister.setText(R.string.complete);
        }
        setSupportActionBar(mToolBar);

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @OnClick(R.id.btn_register)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_register:
                register();
                break;
        }
    }

    private void register() {
        //string.xml里面的字符串怎么取出来,color怎么取出来
        /*getResources().getString(R.string.app_name);
        getResources().getColor(R.color.c_33000000);*/
        //trim(),去除字符串首尾的空格
        String name = mEtName.getText().toString().trim();
        String psd = mEtPsd.getText().toString().trim();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(psd)) {
            //只要有一个空(null,"")
            showToast(BaseApp.getRes().getString(R.string.name_or_psd_not_null));
            return;
        }

        if (mType == TYPE_REGISTER){
            //账号密码注册
            mPresenter.register(name, psd);

        }else {
            //三方在注册,也叫完善资料
            mPresenter.registerAccess(name,psd,mUid,mTypeId);
        }


    }

    @Override
    public void registerSuccess() {
        ///注册成功后只需要退出当前注册页面即可
        finish();
    }

    @Override
    public void loginSuccess() {
        showToast(BaseApp.getRes().getString(R.string.login_success));
        //完善资料之后登陆成功,结束当前页面,跳转到主页面
        go2MainActivity();
        finish();
        //应该把登录页面也finish掉,怎么通知 ??? Evenbus,广播,handler,
        EventBus.getDefault().post(new FinishEvent());
    }

    private void go2MainActivity() {
        MainActivity.startAct(this);
    }
}
